<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8" isELIgnored="false"%>
<%@ include file="/WEB-INF/views/shared/taglib.jsp"%>
<%
	request.setAttribute("modelConfigPath", "stock");
%>
<%@ include file="/WEB-INF/views/shared/master/contentHeader.jsp"%>
<style type="text/css">

	td input{height:21px; width:98%;border:none;}
	td{height:21px;padding: 2px 2px !important;text-align:center }
</style>
<t:panel cssClass="panel-flat form-detailed" customBody="true" customHead="true">
    <div class="detailed-navbar-content panel-body">
 
        <t:form modelAttribute="inStock" action="/purchase/purchase/saveOutStock" method="post" cssClass="form-horizontal">
            <div class="hide">
                <t:hidden path="inStockId"/>
                <t:hidden path="purchaseId" value='${purchase.purchaseId }'/>
             
                <t:hidden path="deleteMark"/>     
            </div>            
				<t:formgroup >
	                <label for="purchaseId"  class="col-sm-1 control-label cursor-pointer text-right">采购单号</label>
	               	<div class="col-sm-2">
	               	<input id="purchaseId"   readonly="true" class="form-control custom-input" data-control-type="input" data-ts-forcestepdivisibility="none" type="text" value="${purchase.purchaseNo }" placeholder="请输入关联采购单号">			               
	               	</div>
	               	<t:label path="operatorName" cssClass="col-sm-1" />
	                <t:input path="operatorName" colClass="col-sm-2" />
	                <t:label path="inStockNo" cssClass="col-sm-1" />
	                <t:input path="inStockNo" colClass="col-sm-2" /> 
	                <t:label path="createDate" cssClass="col-sm-1" />
               		<t:datetime2  inputGroupIcon="icon-calendar" path="createDate" colClass="col-sm-2"></t:datetime2>
	                <!--  <t:label path="warehouse" cssClass="col-sm-1" />
	                <t:select2 path="warehouse" allowClear="true" placeholder="请选择仓库"  colClass="col-xs-4" /> -->
	            </t:formgroup>	
	             <t:formgroup>
	            	
	                <t:label path="comment" cssClass="col-sm-1" />
	               	<t:textarea  path="comment" colClass="col-md-11"/>      
	            </t:formgroup>
				<div class="table-responsive" style="height:340px">
				<table class="cntable table table-bordered  " >	
						
					<thead>
						<tr class="success" >
							<th style="width:60px">序号</th>
							<th style="width:120px">商品编码</th>
							<th style="width:120px">商品名称</th>
							<th style="width:100px">规格</th>
							<th style="width:70px">单位</th>
							<th style="width:100px">剩余可退数</th>
							<th style="width:80px">退货数</th>
							<th style="width:100px">退货单价(元)</th>
							<th style="width:80px">小计(元)</th>
						</tr>
					</thead>
					<tbody >
					<c:forEach var="purchaseCollocation" items="${purchase.purchaseCollocations}" varStatus="status">
						<tr data-surplusNum="${purchaseCollocation.surplusNum  }">
							<td>${status.index+1 }</td>
							<td  class="hide">
								<input id="collocationIds" name="collocationIds" value="${purchaseCollocation.collocation.collocationId}" type="hidden">
							</td>
							<td>${purchaseCollocation.collocation.collocationNo }</td>
							<td>${purchaseCollocation.cargo.cargoName }</td>
							<c:if test="${purchaseCollocation.collocation.propertyNameOne ne null &&purchaseCollocation.collocation.propertyNameTwo ne null }">
								<td>${purchaseCollocation.collocation.propertyNameOne }*${purchaseCollocation.collocation.propertyNameTwo }</td>
							</c:if>
							<c:if test="${purchaseCollocation.collocation.propertyNameOne ne null &&purchaseCollocation.collocation.propertyNameTwo eq null }">
								<td>${purchaseCollocation.collocation.propertyNameOne }</td>
							</c:if>
							<td>${purchaseCollocation.cargo.unit }</td>
							<td>${purchaseCollocation.surplusNum }</td>
							<td class="num">
								<input id="inStockNums" onchange="checkInput(this)"  name="inStockNums" class="form-control custom-input num" data-control-type="input" data-ts-forcestepdivisibility="none" type="number" value="0" placeholder="0">			               
							</td>						
							<td class="price">
								<input id="inStockPrices" onchange="sumMoney(this)"  name="inStockPrices" class="form-control custom-input " data-control-type="input" data-ts-forcestepdivisibility="none" type="number" value="${purchaseCollocation.purchasePrice }" placeholder="0">			               
							</td>
							<td class="sum">
								<input id="inStockNum"  onchange="averageMoney(this)"   class="form-control custom-input " data-control-type="input" data-ts-forcestepdivisibility="none" type="number" value="0" placeholder="0">			               
							</td>
						</tr>
						</c:forEach>	
					</tbody>
					
				</table>     
				  
			</div>

          
        </t:form> 
    </div>
  
	 <t:panelFoot headingBtnCssClass="heading-btn pull-right-important">
		<t:button icon="icon-floppy-disk" onclick="saveData()" laddaType="progress" cssClass="btn-info" csize="sm">保存</t:button>
	</t:panelFoot>
</t:panel>

<script type="text/javascript">

$(function(){
	randomNumber();
	$.each($(".cntable.table tbody tr"),function(){
		var num =  $(this).attr("data-surplusNum");
		
		
		if(num==0){
			$(this).find("input").attr("readonly","true")
		}
	});
	
});
function saveData(lBtn){
	var saveMark=0;
	$.each($(".num input"),function(){
		
		saveMark+=parseInt($(this).val());
	
	});
	
	if(saveMark==0){
		$.notify("无可退货商品!!","warning");
		return;
	}
	
	if (!$("form:first").valid()){
		return;
	}
	lBtn.start();
	$("form:first").submit();
}
function printer(inStockId){


	top.openModal("/purchase/purchase/outStockDetailPrint?key="+inStockId,"inStock_form","采购退货单打印",1200,450);
}

function checkInput(element){
	var surplusNum = $(element).parent().parent().attr("data-surplusNum");
	
	var prices=$(element).parent().siblings(".price").find("input").val();
	
	
	if(surplusNum<$(element).val()){
		$(element).val(surplusNum);
	}
	var sum=prices*$(element).val();
	$(element).parent().siblings(".sum").find("input").val(sum);
	
}

function sumMoney(element){
	var num=$(element).parent().siblings(".num").find("input").val();
	var sum=num*$(element).val();
	$(element).parent().siblings(".sum").find("input").val(sum);
}

function averageMoney(element){
	var num=$(element).parent().siblings(".num").find("input").val();
	
	
	if(num>0){
		var price=$(element).val()/num;
		
		$(element).parent().siblings(".price").find("input").val(price)
	}
}

function randomNumber(){
	const now =new Date();
	
	var  month=now.getMonth()+1;
	var day=now.getDate();
	var hour=now.getHours();
	var minutes=now.getMinutes();
	var seconds=now.getSeconds();
    var inStockNo="CG-R-"+ now.getFullYear().toString() + month.toString() + day + hour + minutes + seconds
	$("#inStockNo").val(inStockNo);
	
}
</script>
<%@ include file="/WEB-INF/views/shared/base/scripts/jqueryui.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formtags.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formbuttons.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formpickers.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formselects.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formstyling.jsp"%>
<%@ include file="/WEB-INF/views/shared/master/dialog.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/notifications.jsp"%>
<%@ include file="/WEB-INF/views/shared/master/contentFooter.jsp"%>
